import {Component, OnInit} from '@angular/core';
import {Service} from '../service/service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {AfterViewInit, Component, ViewChild} from '@angular/core';
import {NzFormatEmitEvent, NzTreeComponent} from 'ng-zorro-antd/tree';

@Component({
  selector: 'app-role',
  templateUrl: './role.component.html',
  styleUrls: ['./role.component.scss']
})
export class RoleComponent implements OnInit {
  @ViewChild('nzTreeComponent', {static: false}) nzTreeComponent: NzTreeComponent;
  defaultCheckedKeys = [];
  defaultSelectedKeys = [];
  defaultExpandedKeys = [];
  private list: Array = [];
  private authList: Array = [];
  public edit: object = {};
  visible = false;
  visibleAuth = false;
  constructor(private service: Service, public msg: NzMessageService) {
    this.getRole();
  }

  ngOnInit(): void {
  }

  private async getRole() {
    const result: object = await this.service.getRole(null);
    this.list = result.data.data;
    // console.log(this.list);
  }

  showModal(data) {
    if (data) {
      this.edit = data;
    } else {
      this.edit = {
        title: '', description: ''
      };
    }
    this.visible = true;
  }

  async showModalAuth(v) {
    this.edit = v;
    console.log(this.edit);
    const data = {role_id: this.edit._id};
    const result: object = await this.service.getRoleId(data);
    console.log( result.data.data);
    const d = result.data.data;
    this.authList = this.format(d);
    console.log(this.authList);
    this.visibleAuth = true;
  }

  private format(data) {
    data.forEach((v, i) => {
      v.children = v.items;
      v.title = v.module_name;
      v.key = v._id;
      v.selected = v.checked;
      v.checked = undefined;
      v.expanded = true;
      v.children.forEach((c, idx) => {
        c.title = c.action_name;
        c.checked = c.checked;
        c.isLeaf = true;
        c.key = c._id;
      });
    });
    return data;
  }

  async update() {
    // console.log(this.edit);
    if (this.edit._id) {
      const {_id, title, description} = this.edit;
      const data = {
        _id, title, description
      };
      const result: object = await this.service.editRole(data);
      // console.log(result);
      if (result.data.code === 200) {
        this.msg.info('修改角色信息成功');
        this.getRole();
      }
    } else {
      const {title, description} = this.edit;
      const data = {
        title, description
      };
      const result: object = await this.service.addRole(data);
      // console.log(result);
      if (result.data.code === 200) {
        this.msg.info('添加角色信息成功');
        this.getRole();
      }
    }
    this.close();
  }

  close(): void {
    this.edit = {};
    this.visible = false;
  }

  closeAuth(): void {
    this.visibleAuth = false;
  }

  cancel(): void {

  }

  async confirm(_id): void {
    const data = {_id};
    const result: object = await this.service.deleteRole(data);
    // console.log(result);
    if (result.data.code === 200) {
      this.msg.info('删除用户信息成功');
      this.getRole();
    }
  }

  nzClick(event: NzFormatEmitEvent): void {
    // console.log(event);
  }

  nzCheck(event: NzFormatEmitEvent): void {
    // console.log(event);
  }

  // nzSelectedKeys change
  nzSelect(keys: string[]): void {
    console.log(keys, this.nzTreeComponent.getSelectedNodeList());
  }

  async updateAuth() {
    const data = this.nzTreeComponent.getTreeNodes();
    console.log(data);
    const access_node = [];
    // 此代码用于测试数据是否正确
    // data.forEach(v => {
    //   if (v._isChecked === true) {
    //     nodes.push({action_name: v.title, key: v.key});
    //   }
    //   v._children.forEach(val => {
    //     if (val._isChecked === true) {
    //       nodes.push({action_name: val.title, key: val.key});
    //       // 如果孩子选中了，没选中父亲，那么把父亲也加上去。
    //       if (val._isChecked === true && v._isChecked !== true) {
    //         v._isChecked = true;
    //         nodes.push({action_name: v.title, key: v.key});
    //       }
    //     }
    //   })
    // })
    // 正式开发
    data.forEach(v => {
      if (v._isChecked === true) {
        access_node.push(v.key);
      }
      v._children.forEach(val => {
        if (val._isChecked === true) {
          access_node.push(val.key);
          // 如果孩子选中了，没选中父亲，那么把父亲也加上去。
          if (val._isChecked === true && v._isChecked !== true) {
            v._isChecked = true;
            access_node.push(v.key);
          }
        }
      });
    });
    const d = {
      role_id: this.edit._id,
      access_node
    };
    const result: object = await this.service.auth(d);
    console.log(result);
    if (result.data.code === 200) {
      this.msg.info('角色授权成功');
      this.getRole();
      this.closeAuth();
    }
  }
}
